//
//  FirstPageViewController.m
//  你的温实初
//
//  Created by lanou on 16/7/12.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "FirstPageViewController.h"
@class FirstPageListViewController;
@interface FirstPageViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) HeanderView *headerview;

@property (nonatomic, strong) UITableView *tableview;

@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, assign) NSInteger indexPath;

@property (nonatomic, strong) NSMutableArray *doubleCellArray;

@property (nonatomic, strong) NSString *leftUrl;

@property (nonatomic, strong) NSString *rightUrl;

@property (nonatomic, assign) NSInteger newStart;

@end

@implementation FirstPageViewController

- (NSMutableArray *)doubleCellArray {
    if (!_doubleCellArray) {
        _doubleCellArray = [NSMutableArray new];
    }
    return _doubleCellArray;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    return _dataSource;
}

- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kwidth, kheight) style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.bounces = NO;
        _tableview.bouncesZoom = NO;
        _tableview.showsVerticalScrollIndicator = NO;
    }
    return  _tableview;
}

- (HeanderView *)headerview {
    if (!_headerview) {
        NSMutableArray *imgArray = [NSMutableArray new];
        for (int i = 0; i < 4; i ++) {
            UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"s%d.jpg", i+1]];
            [imgArray addObject:img];
        }
        _headerview = [[HeanderView alloc] initWithFrame:CGRectMake(0, 0, kwidth, kheight/5*4) ImageArray:imgArray];
    }
    return _headerview;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.title = @"首页";
    self.view.backgroundColor = [UIColor cyanColor];
    
    [self loadTableViewData];
    
    [self.tableview registerNib:[UINib nibWithNibName:@"FirstTextCell" bundle:nil] forCellReuseIdentifier:@"firsttext_cell"];
    
    [self.tableview registerNib:[UINib nibWithNibName:@"FirstImageCell" bundle:nil] forCellReuseIdentifier:@"firstimage_cell"];
    
    [self.tableview registerNib:[UINib nibWithNibName:@"FirstDoubleCell" bundle:nil] forCellReuseIdentifier:@"firstdouble_cell"];
    
    
    
}

- (void)viewDidAppear:(BOOL)animated {
    
    [self.view addSubview:self.tableview];
    self.tableview.tableHeaderView = self.headerview;
//    __weak typeof(self) weakSelf = self;
    [self.tableview mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(64);
        make.left.right.bottom.mas_equalTo(0);
    }];
    
    self.headerview.FirstPage = self;
}

- (void)loadTableViewData {
    // 请求数据
    [RequestManager fetchDataWithUrl:kFirstUrl Parameter:@{} Method:@"GET" Compelet:^(NSData *data) {
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        for (NSDictionary *dic in bigDic[@"data"][@"items"]) {
            FirstModel *model = [FirstModel new];
            [model setValuesForKeysWithDictionary:dic];
            if ([model.show_type isEqual:@(8)]) {
                [self.doubleCellArray addObject:model];
                
            } else{
                
                if (self.doubleCellArray.count!=0) {
                    
                    [self.dataSource addObject:[NSArray arrayWithArray:self.doubleCellArray]];
                    
                    [self.doubleCellArray removeAllObjects];
                }
                [self.dataSource addObject:model];
            }
        }
    } updateUI:^{
        [self.tableview reloadData];
        NSLog(@"%ld", self.dataSource.count);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -------tableview--------

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FirstModel *model = self.dataSource[indexPath.row];
    
    if ([self.dataSource[indexPath.row] isKindOfClass:[NSArray class]]) {
        NSArray *array = self.dataSource[indexPath.row];
        FirstDoubleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"firstdouble_cell" forIndexPath:indexPath];
        [cell.leftButton addTarget:self action:@selector(cellLeftAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell.rightButton addTarget:self action:@selector(cellRightAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell setValueWithArray:array];
        FirstModel *model = array.firstObject;
        FirstModel *submodel = array.lastObject;
        self.leftUrl = model.url;
        self.rightUrl = submodel.url;
        array = [NSArray new];
//        cell.userInteractionEnabled = NO;
        return cell;
    } else if ([model.show_type isEqual: @(1)]) {
            FirstImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"firstimage_cell" forIndexPath:indexPath];
            [cell setValueWithModel:model];
            return cell;
        } else if ([model.show_type isEqual:@(2)]) {
            FirstTextCell *cell = [tableView dequeueReusableCellWithIdentifier:@"firsttext_cell" forIndexPath:indexPath];
            [cell setValueWithModel:model];
            return cell;
        }

    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 200;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    [tableView cellForRowAtIndexPath:indexPath];
    FirstModel *model = self.dataSource[indexPath.row];
    FirstPageListViewController *FPListVC = [FirstPageListViewController new];
    if ([self.dataSource[indexPath.row] isKindOfClass:[NSArray class]]) {
        NSArray *array = self.dataSource[indexPath.row];
        FirstModel *model = array.firstObject;
        FirstModel *submodel = array.lastObject;
        self.leftUrl = model.url;
        self.rightUrl = submodel.url;
        
    } else if ([model.show_type  isEqual: @(1)]) {
        FPListVC.url = model.url;
        [self.navigationController pushViewController:FPListVC animated:YES];
    } else if ([model.show_type isEqual:@(2)]) {
        FPListVC.url = model.url;
        [self.navigationController pushViewController:FPListVC animated:YES];
    }
}

- (void)cellLeftAction:(UIButton *)sender {
    FirstPageListViewController *fplVC = [FirstPageListViewController new];
    fplVC.url = self.leftUrl;
    [self.navigationController pushViewController:fplVC animated:YES];
}

- (void)cellRightAction:(UIButton *)sender {
    FirstPageListViewController *fplVC = [FirstPageListViewController new];
    fplVC.url = self.rightUrl;
    [self.navigationController pushViewController:fplVC animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
